package com.clwl.supervise.superviseapp.controller.base;

import com.clwl.supervise.superviseapp.entity.base.SysAppUpdate;
import com.clwl.supervise.superviseapp.entity.result.ResultEntity;
import com.clwl.supervise.superviseapp.service.base.AppUpdateService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 作者姓名:cheny
 * 创建时间:2022/5/18 16:50
 * 说明信息:
 **/
@RestController
@RequestMapping("/update")
public class AppUpdateController {

    @Resource
    private AppUpdateService updateService;


    /**
     * 检测版本更新
     *
     * @return
     */
    @GetMapping("checkVersion")
    public ResultEntity checkUpdate(String type) {
        SysAppUpdate sysAppUpdate = updateService.selectTopDateUpdate(type,null);
        if (sysAppUpdate == null) {
            return ResultEntity.success();
        } else {
            return ResultEntity.success(sysAppUpdate);
        }
    }

    /**
     * 获取最新版本的下载安装包
     * @param type ios/android
     * @return
     */
    @GetMapping("getNewVersion")
    public void getNewVersion(HttpServletRequest request, HttpServletResponse response, String type) throws Exception {
        SysAppUpdate sysAppUpdate = updateService.selectTopDateUpdate(type,"1");
        if(sysAppUpdate != null) {
            String protocol = request.getHeader("X-Forwarded-Proto");
            //获取数据库文件信息
            String filePath = sysAppUpdate.getFileUrl();
            String requestURI =  protocol + "://" + request.getServerName() + ":8081" +  request.getContextPath();
            response.sendRedirect(requestURI + "/"+filePath);
        }
    }

}
